package com.ruoyi.system.service.impl;

import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.core.redis.RedisKey;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.SysGroup;
import com.ruoyi.system.domain.CrmUser;
import com.ruoyi.system.mapper.SysGroupMapper;
import com.ruoyi.system.service.ISysGroupService;
import com.ruoyi.system.service.ICrmUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 【请填写功能名称】Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-01-05
 */
@Service
public class SysGroupServiceImpl implements ISysGroupService
{
    @Autowired
    private SysGroupMapper sysGroupMapper;

    @Autowired
    private ICrmUserService crmCustomerServiceImpl;
    @Autowired
    private RedisCache redisCache;

    /**
     * 查询【请填写功能名称】
     * 
     * @param id 【请填写功能名称】ID
     * @return 【请填写功能名称】
     */
    @Override
    public SysGroup selectSysGroupById(Long id)
    {
        return sysGroupMapper.selectSysGroupById(id);
    }

    /**
     * 查询【请填写功能名称】列表
     * 
     * @param SysGroup 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<SysGroup> selectSysGroupList(SysGroup SysGroup)
    {
        List<SysGroup> SysGroups= redisCache.getCacheList(RedisKey.GEO_SYSTEM_GROUP+SysGroup.getCreateUser());
        if(SysGroups.isEmpty()){
            SysGroups= sysGroupMapper.selectSysGroupList(SysGroup);
        }
        return SysGroups;
    }

    /**
     * 新增【请填写功能名称】
     * 
     * @param SysGroup 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertSysGroup(SysGroup SysGroup)
    {
        redisCache.deleteObject(RedisKey.GEO_SYSTEM_GROUP+SysGroup.getCreateUser());
        Long userId = SecurityUtils.getLoginUser().getUser().getUserId();
        SysGroup.setCreateTime(DateUtils.getNowDate());
        SysGroup.setCreateUser(userId);
        return sysGroupMapper.insertSysGroup(SysGroup);
    }

    /**
     * 修改【请填写功能名称】
     * 
     * @param SysGroup 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateSysGroup(SysGroup SysGroup)
    {
        redisCache.deleteObject(RedisKey.GEO_SYSTEM_GROUP+SysGroup.getCreateUser());
        return sysGroupMapper.updateSysGroup(SysGroup);
    }

    /**
     * 批量删除【请填写功能名称】
     * 
     * @param ids 需要删除的【请填写功能名称】ID
     * @return 结果
     */
    @Override
    public int deleteSysGroupByIds(Long[] ids)
    {
        return sysGroupMapper.deleteSysGroupByIds(ids);
    }

    /**
     * 删除【请填写功能名称】信息
     * 
     * @param id 【请填写功能名称】ID
     * @return 结果
     */
    @Override
    public int deleteSysGroupById(Long id)
    {
        Long userId = SecurityUtils.getLoginUser().getUser().getUserId();
        CrmUser param=new CrmUser();
        param.setCreateUser(userId);
        param.setCusGroupId(id);
        List<CrmUser> crmCustomers = crmCustomerServiceImpl.selectCrmCustomerList(param);
        //优化为一个sql 直接将群组为这个id的客户群组属性设置为null
        for (CrmUser cus : crmCustomers
             ) {
            cus.setCusGroupId(null);
            crmCustomerServiceImpl.updateCrmCustomer(cus);
        }
        return sysGroupMapper.deleteSysGroupById(id);
    }

}
